// 自定义防抖函数
function debounce(func, wait) {
  let timeout;
  return function() {
    const context = this;
    const args = arguments;
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(context, args);
    }, wait);
  };
}

Page({
  data: {
    searchKeyword: '', // 搜索关键词
    banks: [
      {
        name: '中国工商银行',
        icon: '/assets/bank-logo/工商银行.png',
      },
      {
        name: '中国农业银行',
        icon: '/assets/bank-logo/农行.png',
      },
      {
        name: '中国银行',
        icon: '/assets/bank-logo/中国银行.png',            
      },
      {
        name: '中国建设银行',
        icon: '/assets/bank-logo/建设银行.png',            
      },
      {
        name: '招商银行',
        icon: '/assets/bank-logo/招商银行.png',            
      },
      {
        name: '交通银行',
        icon: '/assets/bank-logo/交通银行.png',            
      },
      {
        name: '中国邮政储蓄银行',
        icon: '/assets/bank-logo/中国邮政储蓄银行.png',            
      },
      {
        name: '中国民生银行',
        icon: '/assets/bank-logo/中国民生银行.png',            
      },
      {
        name: '兴业银行',
        icon: '/assets/bank-logo/兴业银行.png',
      },
      {
        name: '平安银行',
        icon: '/assets/bank-logo/平安银行.png'
      },
      {
        name: '浦发银行',
        icon: '/assets/bank-logo/浦发银行.png'
      },
      {
        name: '上海银行',
        icon: '/assets/bank-logo/上海银行.png'
      },
      {
        name: '中信银行',
        icon: '/assets/bank-logo/中信银行.png'
      },
      {
        name: '广发银行',
        icon: '/assets/bank-logo/广发银行.png'
      },
      {
        name: '中国光大银行',
        icon: '/assets/bank-logo/中国光大银行.png'
      },
      {
        name: '华夏银行',
        icon: '/assets/bank-logo/华夏银行.png'
      },
      {
        name: '海南农商银行',
        icon: '/assets/bank-logo/海南农商银行.png'
      },
      {
        name: '江南农村商业银行',
        icon: '/assets/bank-logo/江南农村商业银行.png'
      },
      {
        name: '江苏江阴农村商业银行',
        icon: '/assets/bank-logo/江苏江阴农村商业银行.png'
      },
      {
        name: '莱商银行',
        icon: '/assets/bank-logo/莱商银行logo.png'
      },
      {
        name: '西安银行',
        icon: '/assets/bank-logo/西安银行.png'
      },
      {
        name: '临商银行',
        icon: '/assets/bank-logo/临商银行.png'
      },
      {
        name: '山西农信',
        icon: '/assets/bank-logo/山西农信.png'
      },
      {
        name: '深圳农村商业银行',
        icon: '/assets/bank-logo/深圳农村商业银行.png'
      },
      {
        name: '安徽省农村信用社',
        icon: '/assets/bank-logo/安徽省农村信用社联合社.png'
      },
      {
        name: '重庆银行',
        icon: '/assets/bank-logo/重庆银行.png'
      },
      {
        name: '汉口银行',
        icon: '/assets/bank-logo/汉口银行.png'
      },
      {
        name: '东莞农村商业银行',
        icon: '/assets/bank-logo/东莞农村商业银行.png'
      },
      {
        name: '北京银行',
        icon: '/assets/bank-logo/北京银行.png'
      },
      {
        name: '贵州省农村信用社联合社',
        icon: '/assets/bank-logo/贵州省农村信用社联合社.png'
      },
      {
        name: '福建省农村信用社联合社',
        icon: '/assets/bank-logo/福建省农村信用社联合社.png'
      },
      {
        name: '北京农商银行',
        icon: '/assets/bank-logo/北京农商银行.png'
      },
      {
        name: '广州农村商业银行',
        icon: '/assets/bank-logo/广州农村商业银行.png'
      },
      {
        name: '甘肃银行',
        icon: '/assets/bank-logo/甘肃银行.png'
      },
      {
        name: '顺德农商银行',
        icon: '/assets/bank-logo/顺德农商银行.png'
      },
      {
        name: '天津滨海农村商业银行',
        icon: '/assets/bank-logo/天津滨海农村商业银行.png'
      },
      {
        name: '吉林银行',
        icon: '/assets/bank-logo/吉林银行.png'
      },
      {
        name: '张家港农村商业银行',
        icon: '/assets/bank-logo/张家港农村商业银行.png'
      },
      {
        name: '中原银行',
        icon: '/assets/bank-logo/中原银行.png'
      },
      {
        name: '银座村镇银行',
        icon: '/assets/bank-logo/北京顺义银座村镇银行logo.png'
      },
      {
        name: '桂林银行',
        icon: '/assets/bank-logo/桂林银行.png'
      },
      {
        name: '张家口银行',
        icon: '/assets/bank-logo/张家口银行.png'
      },
      {
        name: '南京银行',
        icon: '/assets/bank-logo/南京银行.png'
      },
      {
        name: '天津农商银行',
        icon: '/assets/bank-logo/天津农商银行.png'
      },
      {
        name: '成都农商银行',
        icon: '/assets/bank-logo/成都农商银行.png'
      },
      {
        name: '德州银行',
        icon: '/assets/bank-logo/德州银行.png'
      },
      {
        name: '贵阳银行',
        icon: '/assets/bank-logo/贵阳银行.png'
      },
      {
        name: '杭州银行',
        icon: '/assets/bank-logo/杭州银行.png'
      },
      {
        name: '昆仑银行',
        icon: '/assets/bank-logo/昆仑银行.png'
      },
      {
        name: '重庆农村商业银行',
        icon: '/assets/bank-logo/重庆农村商业银行.png'
      },
      {
        name: '承德银行',
        icon: '/assets/bank-logo/承德银行.png'
      },
      {
        name: '浙江民泰商业银行',
        icon: '/assets/bank-logo/浙江民泰商业银行.png'
      },
      {
        name: '台州银行',
        icon: '/assets/bank-logo/台州银行.png'
      },
      {
        name: '郑州银行',
        icon: '/assets/bank-logo/郑州银行.png'
      },
      {
        name: '河北银行',
        icon: '/assets/bank-logo/河北银行.png'
      },
      {
        name: '锦州银行',
        icon: '/assets/bank-logo/锦州银行.png'
      },
      {
        name: '浙江泰隆商业银行',
        icon: '/assets/bank-logo/浙江泰隆商业银行.png'
      },
      {
        name: '绍兴银行',
        icon: '/assets/bank-logo/绍兴银行.png'
      },
      {
        name: '宁夏银行',
        icon: '/assets/bank-logo/宁夏银行.png'
      },
      {
        name: '晋商银行',
        icon: '/assets/bank-logo/晋商银行.png'
      },
      {
        name: '恒丰银行',
        icon: '/assets/bank-logo/恒丰银行.png'
      },
      {
        name: '上海农商银行',
        icon: '/assets/bank-logo/上海农商银行.png'
      },
      {
        name: '日照银行',
        icon: '/assets/bank-logo/日照银行.png'
      },
      {
        name: '东营银行',
        icon: '/assets/bank-logo/东营银行.png'
      },
      {
        name: '厦门银行',
        icon: '/assets/bank-logo/厦门银行.png'
      },
      {
        name: '南洋商业银行',
        icon: '/assets/bank-logo/南洋商业银行.png'
      },
      {
        name: '青岛银行',
        icon: '/assets/bank-logo/青岛银行.png'
      },
      {
        name: '齐鲁银行',
        icon: '/assets/bank-logo/齐鲁银行.png'
      },
      {
        name: '齐商银行',
        icon: '/assets/bank-logo/齐商银行.png'
      },
      {
        name: '渤海银行',
        icon: '/assets/bank-logo/渤海银行.png'
      },
      // 可以根据需要添加更多银行数据
    ],
    filteredBanks: []
  },
  
  onLoad() {
    // 初始化时显示全部银行
    this.setData({
      filteredBanks: this.data.banks
    });
  },
  
  // 搜索输入处理函数（使用防抖优化性能）
  onLoad() {
    // 初始化时显示全部银行
    this.setData({
      filteredBanks: this.data.banks
    });
  },
  
  // 搜索输入处理函数（使用自定义防抖）
  handleSearch: debounce(function(e) {
    const inputValue = e.detail.value.trim().toLowerCase();
    console.log('搜索关键词:', inputValue);
    
    if (!inputValue) {
      this.setData({
        filteredBanks: this.data.banks,
        searchKeyword: ''
      });
      return;
    }
    
    const filtered = this.data.banks.filter(bank => 
      bank.name.toLowerCase().includes(inputValue)
    );
    
    this.setData({
      filteredBanks: filtered,
      searchKeyword: inputValue
    });
  }, 300),
  
  // 添加银行卡
  addCard(e) {
    const index = e.currentTarget.dataset.index;
    const selectedBank = this.data.filteredBanks[index];
    
    // 判断是否模拟器环境
    const isSimulator = typeof my === 'object' && my.isIDE;
    
    if (isSimulator) {
      my.navigateTo({
        url: `/pages/add-card-simulate/index?bankName=${encodeURIComponent(selectedBank.name)}&bankIcon=${encodeURIComponent(selectedBank.icon)}`
      });
    } else {
      my.addCardAuth({
        cardType: 'bank',
        success: (res) => {
          // 真实环境处理逻辑...
        }
      });
    }
  }
})  